package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

import pojo.User;
import util.DBUtils;
import dao.UserDao;

public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {
	
	

	@Override
	public int add(User user) {
		String sql = "insert into users values(?,?,?)";
		PreparedStatement pst = null;
		int m = 0;
		Connection conn = DBUtils.createConnection();
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, UUID.randomUUID().toString());
			pst.setString(2, user.getUname());
			pst.setString(3, user.getPsd());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			logger.error(e);
		}finally{
			DBUtils.close(conn, pst, null);
		}
		return m;
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int update(User t) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public User load(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> findAll() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public User findByName(String name) {
		String sql = "select * from users where uname=?";
		Connection conn = DBUtils.createConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		User user =null;
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, name);
			rs = pst.executeQuery();
			if (rs.next()) {
				user = new User();
				user.setId(rs.getString("id"));
				user.setUname(rs.getString("uname"));
				user.setPsd(rs.getString("psd"));
			}
		} catch (Exception e) {
			logger.error(e);
		}finally{
			DBUtils.close(conn, pst, rs);
		}
		return user;	
	}



}
